import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '../utils/storage'

// 一级路由
import Register from '@/views/RegisterA'
import Login from '@/views/LoginA'
import Layout from '@/views/LayoutA'

// 二级路由
import Article from '@/views/second/articleA'
import Find from '@/views/second/findA'
import News from '@/views/second/newsA'
import User from '@/views/second/userA'

// 三级路由
import Favorate from '@/views/favorateA'
import Rent from '@/views/rentA'
import City from '@/views/third/cityA'
import Gorent from '@/views/third/goRent'
import Map from '@/views/third/mapA'
import rentSearch from '@/views/third/rentSearch'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Layout,
    redirect: '/article',
    children: [
      {
        path: '/article',
        component: Article
      },
      {
        path: '/find',
        component: Find
      },
      {
        path: '/news',
        component: News
      },
      {
        path: '/user',
        component: User
      },
      {
        path: '/favorate',
        component: Favorate
      },
      {
        path: '/rent',
        component: Rent
      }
    ]
  },
  {
    path: '/register',
    component: Register
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/gorent',
    component: Gorent
  },
  {
    path: '/city',
    component: City
  },
  {
    path: '/map',
    component: Map
  },
  {
    path: '/rentsearch',
    component: rentSearch
  }

]

const router = new VueRouter({
  routes
})

const WhiteList = ['/login', '/register']
router.beforeEach((to, from, next) => {
  console.log(to, from)
  next()
  if (getToken()) {
    next()
  } else {
    if (WhiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
